<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            查询配送单
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;padding-bottom: 50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">任务类型</div>
                        <select id="taskType" style="font-size: 0.7rem;">
                        </select>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">源仓库</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 源仓库" id="workCenterCodeY" value=""
                                onclick="getIdSelector();" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenterY()"></i>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">目标仓库</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 仓库" id="workCenterCode" value=""
                                onclick="getIdSelector();" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenter()"></i>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">调入库位</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 库位" id="workCellCode" value=""
                                onclick="getIdSelector();" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell()"></i>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width:150px">任务创建日期</div>
                        <div class="aui-list-item-input" onclick="timePicker();">
                            <input type="text" placeholder="选择日期" style="padding-left: 35px;" class="aui-input"
                                id="dateTime">
                        </div>
                    </div>
                </li>
            </ul>
            <div class="aui-btn aui-btn-info aui-btn-block" onclick="search()">查询</div>
        </div>
        <ul class="ui-selectee" id="tbodyHtml">

        </ul>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-btn aui-btn-info aui-btn-block" style="margin:0 5%;width: 90%;" onclick="goPage()">带回</div>

    </footer>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    var workCenterGid = ""; //仓库GID
    var workCenterCode = ""; //仓库编码
    var workCenterName = ""; //仓库名称
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称

    var workCenterGidY = ""; //源仓库GID
    var workCenterCodeY = ""; //源仓库编码
    var workCenterNameY = ""; //源仓库名称

    var list = [];
    var arrSave = [];//要带回的单据号

    //初始化查询
    apiready = function () {
        search();
        //查询条码解析规则
        initScanRule();
        selectTaskType();
    }
    function selectTaskType() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUdiList";
        var data = {
            values: {
                code: "taskType",
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("taskType"), html);

            }
        });
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //1：仓库
            if (idSelector == 'workCenterCode') {

                $("#workCenterCode").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector);

                //2:库位
            } else if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

                //扫描条码
            } else if (idSelector == 'distributeTaskCode') {
                $("#distributeTaskCode").val(ret);
                tmSplit2(ret);
            } else if (idSelector == 'workCenterCodeY') {

                $("#workCenterCodeY").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector);

                //2:库位
            }


        });
    }

    //6：选择物料
    function goSelectMrl() {
        api.openWin({
            name: 'uwmSelectMrl',
            url: 'uwmSelectMrl.html',
            pageParam: {
                name: "paSearchTask"
            }
        });
    }


    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                    console.log("scanRule:" + scanRule);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }


    //扫描之后条码解析
    function tmSplit2(sm) {
        var arr = sm.split(",");
        $("#distributeTaskCode").val(sm);

        if (arr[0] == null || arr[0] == undefined) {
            $("#distributeTaskCode").val("");
        } else {
            $("#distributeTaskCode").val(arr[0]);
        }

    }

    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#dateTime").val(date);
            }
        });
    }


    //1：仓库扫描校验
    function getWorkCenter(code, inputId) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCenter";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCenterCode: code,
                mrlCode: "",
                lotCode: "",
                qty: "",
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCenterGid = list[0].workCenterGid;
                    workCenterCode = list[0].workCenterCode;
                    workCenterName = list[0].workCenterName;
                    document.getElementById(inputId).value = list[0].workCenterCode + ":" + list[0].workCenterName;
                    $("#workCellCode").focus();
                    idSelector = "workCellCode";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCenterCode").focus();
                    $("#workCenterCode").select();
                }
            }
        });
    }


    //选中仓库
    function goSelectWorkCenter() {
        api.openWin({
            name: 'uwmSelectWorkCenter',
            url: 'uwmSelectWorkCenter.html',
            pageParam: {
                flag: "in",
                name: "paSearchTask",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }

    //仓库选择后回调函数
    function setWorkCenter(id, code, name) {
        workCenterGid = id;
        workCenterCode = code;
        workCenterName = name;
        //$("#workCenterCode").attr("value",code+":"+name);
        document.getElementById('workCenterCode').value = code + ":" + name;
        $("#workCellCode").focus();
        idSelector = "workCellCode";
    }

    //PDA自带扫描回车事件
    $("#workCenterCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCenterCode").val(sm);
            // console.log("扫描回车事件：" + sm);
            //校验改箱码是否存在
            getWorkCenter(sm, "workCenterCode");
        }
    });


    //选中源仓库
    function goSelectWorkCenterY() {
        api.openWin({
            name: 'uwmSelectWorkCenterY',
            url: 'uwmSelectWorkCenterY.html',
            pageParam: {
                flag: "in",
                name: "paSearchTask",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }

    //仓库选择后回调函数
    function setWorkCenterY(id, code, name) {
        workCenterGidY = id;
        workCenterCodeY = code;
        workCenterNameY = name;
        //$("#workCenterCode").attr("value",code+":"+name);
        document.getElementById('workCenterCodeY').value = code + ":" + name;
    }

    //PDA自带扫描回车事件
    $("#workCenterCodeY").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCenterCodeY").val(sm);
            // console.log("扫描回车事件：" + sm);
            //校验改箱码是否存在
            getWorkCenter(sm, "workCenterCode");
        }
    });



    //库位扫描校验
    function getWorkCell(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: workCenterGid, //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    backtype(workCellGid)

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }
            }
        });
    }


    //选择库位
    function goSelectWorkCell() {
        var tm = $("#tm").val();
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "in_rk",
                name: "paSearchTask",
                tm: tm,
                workCenterGid: workCenterGid,
                relWorkCellGid: "",
                workCellFlag: ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name) {
        workCellGid = id;
        workCellCode = code;
        workCellName = name;
        document.getElementById('workCellCode').value = code + ":" + name;
    }

    //PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCell(sm, "workCellCode");
        }
    });


    function search() {
        var dateTime = $("#dateTime").val();
        var taskType = $("#taskType").val();
        if (taskType == null) {
            taskType = "";
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchPataskCode";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCenterCode: workCenterCode,
                workCenterCodeY: workCenterCodeY,
                workCellCode: workCellCode,
                taskType: taskType,
                un: un,
                createDate: dateTime
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    list = myobj.list;
                    setTable(list)
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                }
            }
        });
    }


    function setTable(list) {
        var html = "";
        for (var i = 0; i < list.length; i++) {
            html += "<li class='sortable ui-selectee ui-sortable' >";
            html += "<div class='white_background'>"
            html += "<ul class='aui-list aui-media-list ui-selectee'>";
            html += "<li class='aui-list-item  ui-selectee' style='background-color:" + list[i].backGroundColor + "  !important;'>";
            html += "<div class='aui-media-list-item-inner ui-selectee' >";
            html += "<div class='aui-list-item-inner ui-selectee'>";

            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>配送单号:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + list[i].distributeTaskCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>任务类型:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + list[i].taskType + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>源仓库:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + list[i].workCenterCodeY + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>目标仓库:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + list[i].workCenterCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>调入库位:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + list[i].workCellCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>创建时间:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + list[i].create_date + "</div>";
            html += "<div class='aui-list-item-right aui-btn ui-selectee' style='background:#d0d0d0;color:#000000;' onclick='checkIn(\""
                + list[i].distributeTaskCode + "\",\"" + list[i].str + "\",\"" + list[i].workCenterCodeY + "\",\"" + list[i].workCellCode + "\");'>" + list[i].str + "</div>";
            html += "</div>";

            html += "</div>";
            html += "</div>";
            html += "</li>";
            html += "</ul>";
            html += "</div>";
            html += "</li>";
        }
        $api.html($api.byId("tbodyHtml"), html);
    };

    //选中
    function checkIn(distributeTaskCode, str, workCenterCodeY, workCellCode) {
        for (var i = 0; i < arrSave.length; i++) {
            if (workCenterCodeY != arrSave[i].workCenterCodeY) {
                api.toast({
                    msg: '源仓库不一致，不允许一起选中！',
                    duration: 3000
                });
                return;
            }
            if (workCellCode != arrSave[i].workCellCode) {
                api.toast({
                    msg: '调入库位不一致，不允许一起选中！',
                    duration: 3000
                });
                return;
            }
        }
        for (var i = 0; i < list.length; i++) {
            if (list[i].distributeTaskCode == distributeTaskCode && str == "选中") {
                list[i].backGroundColor = "#00BB00";
                list[i].str = "取消";
                var model = {
                    distributeTaskCode: distributeTaskCode,//配送单号
                    workCellCode: list[i].workCellCode,//调入库位
                    workCenterCodeY: list[i].workCenterCodeY//源仓库
                }
                arrSave.push(model);

            }

            if (list[i].distributeTaskCode == distributeTaskCode && str == "取消") {
                list[i].backGroundColor = "#addbef";
                list[i].str = "选中";
                for (var j = 0; j < arrSave.length; j++) {
                    if (distributeTaskCode == arrSave[j].distributeTaskCode) {
                        arrSave.splice(j, 1)
                    }
                }
            }
        }
        setTable(list);
    };


    //选则返回函数
    function goPage() {
        api.closeWin();
        var name = api.pageParam.name;
        var str = "";
        for (var j = 0; j < arrSave.length; j++) {
            str = str + arrSave[j].distributeTaskCode + ",";

        }
        api.execScript({
            name: name,
            script: 'setPaTaskCode("' + str + '")'
        });

    }


    //返回按钮
    function closeWin() {
        api.closeWin({});
    }

</script>


</html>